package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.UacAddress;
import com.csun.cmny.provider.model.domain.UacSecondLvelMerchantBind;
import com.csun.cmny.provider.model.dto.address.AddressSaveDto;
import com.csun.cmny.provider.model.dto.address.AddressUpdateDto;
import com.csun.cmny.provider.model.dto.dept.EmpQueryDto;
import com.csun.cmny.provider.model.dto.merchant.MerchantPersonageSelectDto;
import com.csun.cmny.provider.model.vo.merchant.MerchantVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.UacAddressService;
import com.csun.cmny.provider.service.UacMerchantService;
import com.csun.cmny.provider.service.UacSecondLvelMerchantBindService;
import com.csun.cmny.provider.utils.LocationUtils;
import com.csun.cmny.util.RedisKeyUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.security.core.parameters.P;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;

import static java.util.Map.Entry.comparingByValue;
import static java.util.stream.Collectors.toMap;

@RestController
@RequestMapping(value = "/address", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UacAddressController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private UacAddressService addressService;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;




    /**
     * 新增地址
     */
    @ValidateAnnotation
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addAddress(@RequestBody @Valid AddressSaveDto addressSaveDto, BindingResult br) {

        UacAddress uacAddress = new UacAddress();
        BeanUtils.copyProperties(addressSaveDto,uacAddress);
        uacAddress.setUserId(getLoginAuthDto().getUserId());



        int result = addressService.save(uacAddress);

        redisTemplate.opsForValue().set(RedisKeyUtil.getUacAddress(uacAddress.getId().toString()),uacAddress);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /*删除地址*/
    @RequestMapping(value = "/delete",method =  RequestMethod.POST)
    public Wrapper deteteAddress(Long id) {

        int result = addressService.deleteByKey(id);

        redisTemplate.delete(RedisKeyUtil.getUacAddress(id.toString()));

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /*修改地址*/
    @ValidateAnnotation
    @RequestMapping(value = "/update",method =  RequestMethod.POST)
    public Wrapper updateAddress(@RequestBody @Valid AddressUpdateDto addressUpdateDto, BindingResult br) {

        UacAddress uacAddress = new UacAddress();
        BeanUtils.copyProperties(addressUpdateDto,uacAddress);

        redisTemplate.opsForValue().set(RedisKeyUtil.getUacAddress(uacAddress.getId().toString()),uacAddress);

        int result = addressService.update(uacAddress);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /*查询个人地址*/
    @RequestMapping(value = "/select",method =  RequestMethod.POST)
    public Wrapper selectAddress() {

        UacAddress uacAddress = new UacAddress();
        uacAddress.setUserId(getLoginAuthDto().getUserId());

        List<UacAddress> select = addressService.select(uacAddress);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),select);
    }



}
